package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ndboo.reslibrary.R;

import java.util.List;

import a51qindian.com.comprehensive.model.AddressInfo;

/**
 * Author: Li
 * Date: 2017/9/14 0014
 * Description:
 */

public class PopAdapter extends RecyclerView.Adapter<PopAdapter.PopHolder> {

    private LayoutInflater mInflater;

    public void setAddressInfoList(List<AddressInfo> addressInfoList) {
        mAddressInfoList = addressInfoList;
        notifyDataSetChanged();
    }

    private List<AddressInfo> mAddressInfoList;
    private OnItemClickListener mItemClickListener;
    public PopAdapter(Context context){
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public PopHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=mInflater.inflate(R.layout.item_pop,parent,false);
        final PopHolder holder=new PopHolder(view);
        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    mItemClickListener.onClick(v,mAddressInfoList.get(pos).getUid(),mAddressInfoList.get(pos).getName());
                }
            });
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(PopHolder holder, int position) {
        holder.mTxtPop.setText(mAddressInfoList.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return mAddressInfoList==null?0:mAddressInfoList.size();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    static class PopHolder extends RecyclerView.ViewHolder{

        private TextView mTxtPop;
        public PopHolder(View itemView) {
            super(itemView);
            mTxtPop= itemView.findViewById(R.id.txt_pop);
        }
    }

    public interface OnItemClickListener{
        void onClick(View view, String id,String text);
    }
}
